package com.cgy.springboot.service;

import com.cgy.springboot.domain.User;
import com.cgy.springboot.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Date;

@Service
public class UserService {

    @Autowired
    private UserMapper userMapper;
    
    public List<User> getAllUsers() {
        return userMapper.findAll();
    }

    public User getUserByUsername(String username) {
        return userMapper.getUserByUsername(username);
    }
    
    // 通过openId查询用户
    public User getUserByOpenId(String openId) {
        return userMapper.getUserByOpenId(openId);
    }
    
    // 更新用户信息
    public void updateUser(User user) {
        user.setUpdateTime(new Date());
        userMapper.updateUser(user);
    }
    
    // 保存新用户
    public void saveUser(User user) {
        if (user.getCreateTime() == null) {
            user.setCreateTime(new Date());
        }
        if (user.getUpdateTime() == null) {
            user.setUpdateTime(new Date());
        }
        userMapper.saveUser(user);
    }
    
    // 根据ID查询用户
    public User getUserById(Integer id) {
        return userMapper.getUserById(id);
    }
}
